/**
 * ConfigController.java
 * 卢晓峰
 * Email��lxf9601@gmail.com
 * QQ：297051898
 * Created by 2010-7-24
 */
package com.autoabacus.dal.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.autoabacus.core.config.Config;
import com.autoabacus.core.config.ConfigLoader;
import com.autoabacus.core.config.ConfigQueryProvider;
import com.autoabacus.core.config.component.Table;
import com.autoabacus.core.config.exception.ConfigFileNotFoundException;
import com.autoabacus.core.config.provider.QuerySource;
import com.autoabacus.core.config.service.AbstractSpringConfigController;

/**
 * 通用配置控制器
 * @author 卢晓峰
 */
@Controller
public class ConfigController {
	
	private Logger logger = LoggerFactory.getLogger(AbstractSpringConfigController.class);
	private String CONFIG_PATH = "e:\\";
	private ConfigLoader configLoader;
	private Config config = null;
	private ConfigQueryProvider configQueryProvider;
	
	public ConfigController() {
		
	}
	
	@RequestMapping
	public Table getTable(String tableName) throws ConfigFileNotFoundException {
		logger.info("客户端加载表{}配置", tableName);
		loadConfig();
		Table table = config.getTables().get(tableName);
		if (table == null) {
			logger.error("配置表{}不存在", tableName);
		}
		return table;
	}
	
	@RequestMapping
	public Map<String, Table> getTables() throws ConfigFileNotFoundException {
		logger.info("客户端加载所有表配置");
		loadConfig();
		return config.tables;
	}
	
	/**
	 * 获得对象
	 */
	@RequestMapping
	public Object getObject(QuerySource querySource){
		return configQueryProvider.queryObject(querySource);
	}
	

	private void loadConfig() throws ConfigFileNotFoundException {
		if (config == null) {
			config = configLoader.loadConfig(CONFIG_PATH);	
		}
	}
	
	@Resource
	public void setConfigLoader(ConfigLoader configLoader) {
		this.configLoader = configLoader;
	}

	@Resource
	public void setConfigQueryProvider(ConfigQueryProvider configQueryProvider) {
		this.configQueryProvider = configQueryProvider;
	}
	
}
